package sousou;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

//后台，由操作系统选择执行的操作，后台反应相应的结果
public class Background {
    Scanner scanner = new Scanner(System.in);

    //定义一个集合存储用户信息
    ArrayList<Person>arrayList = new ArrayList<>();
    HashMap<String,String>hashMap = new HashMap<>();
    //定义一个数组用来模拟消费的记录
    String[] str = {"与父母打电话，通话200分钟 ","与喜欢的人发消息，发了50条信息 ","上网打游戏花了1GB "};
    //给集合中先创建几个用户
    public void create(){
        //信息
        Person person1 = new Person("13912341234","123","话痨套餐","王五",100,65,400,0,100);
        Person person2 = new Person("13912312312","123","网虫套餐","李四",200,68,0,2048,0);
        Person person3 = new Person("13912345678","123","超人套餐","张三",300,88,200,1024,150);
        arrayList.add(person1);
        arrayList.add(person2);
        arrayList.add(person3);
        //消费记录
        hashMap.put("13912341234","与父母打电话，通话200分钟 与喜欢的人发消息，发了50条信息 与父母打电话，通话200分钟 与喜欢的人发消息，发了50条信息 ");
        hashMap.put("13912312312","上网打游戏花了1GB流量 上网打游戏花了1GB ");
        hashMap.put("13912345678","与喜欢的人发消息，发了50条 与父母打电话，通话200分钟 与喜欢的人发消息，发了50条 上网打游戏花了1GB流量 与喜欢的人发消息，发了50条 ");
    }

    //用户登录的方法
    public boolean login(String phone,String key){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)&&arrayList.get(a).getKey().equals(key)){
                return true;
            }
        }
        return false;
    }

    //手机号注册
    public void enroll(){
        String [] phones = new String[9];
        int c = 1;
        for(int i = 1;i<=3;i++) {
            for (int a = 1; a <= 3; a++) {
                long b = 13900000000l;
                String phone = null;
                boolean flag = false;
                do {
                    phone = (long) (Math.random() * 100000000) + b + "";
                    for (int j = 0; j < arrayList.size(); j++) {
                        if (phone.equals(arrayList.get(j).getKey())) {
                            for(int k = 0;k< phones.length;k++){
                                if(phones[k].equals(phone)){
                                    flag = true;
                                    break;
                                }
                            }
                        } else {
                            flag = false;
                        }
                    }
                } while (flag);
                phones[c-1] = phone;
                System.out.print(c + "." + phone + "  ");
                c++;
            }
            System.out.println();
        }
        System.out.println("请输入你要选择的卡号（1-9）");
        int num = scanner.nextInt();
        System.out.println("1.话痨套餐 2.网虫套餐 3.超人套餐， 请选择套餐（输入序号）");
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                System.out.println("请输入姓名");
                String name = scanner.next();
                System.out.println("请输入密码");
                String key = scanner.next();
                System.out.println("请输入预存话费金额");
                int money = scanner.nextInt();
                if(money<58){
                    do{
                        System.out.println("你预存的手机话费不够本月的固定套餐费，请重新充值");
                        money = scanner.nextInt();
                    }while(money<58);
                }
                Person person = new Person(phones[num-1],key,"话痨套餐",name,money-58,58,0,0,0);
                arrayList.add(person);
                hashMap.put(phones[num-1],"");
                System.out.println("注册成！卡号："+phones[num-1]+"用户名:"+name+" 当前余额:"+(money-58)+"。");
                System.out.println("话痨套餐:通话时间500分钟/月,短信条数30条/月，无上网流量");
                break;
            case 2:
                System.out.println("请输入姓名");
                name = scanner.next();
                System.out.println("请输入密码");
                key = scanner.next();
                System.out.println("请输入预存话费金额");
                money = scanner.nextInt();
                if(money<68){
                    do{
                        System.out.println("你预存的手机话费不够本月的固定套餐费，请重新充值");
                        money = scanner.nextInt();
                    }while(money<68);
                }
                person = new Person(phones[num-1],key,"话痨套餐",name,money-68,68);
                arrayList.add(person);
                hashMap.put(phones[num-1],"");
                System.out.println("注册成！卡号："+phones[num-1]+"用户名:"+name+" 当前余额:"+(money-68)+"。");
                System.out.println("话痨套餐:通话时间无,短信条数无，上网流量3GB/月");
                break;
            case 3:
                System.out.println("请输入姓名");
                name = scanner.next();
                System.out.println("请输入密码");
                key = scanner.next();
                System.out.println("请输入预存话费金额");
                money = scanner.nextInt();
                if(money<78){
                    do{
                        System.out.println("你预存的手机话费不够本月的固定套餐费，请重新充值");
                        money = scanner.nextInt();
                    }while(money<78);
                }
                person = new Person(phones[num-1],key,"话痨套餐",name,money-78,78);
                arrayList.add(person);
                hashMap.put(phones[num-1],"");
                System.out.println("注册成！卡号："+phones[num-1]+"用户名:"+name+" 当前余额:"+(money-78)+"。");
                System.out.println("话痨套餐:通话时间200分钟/月,短信条数50条/月，上网流量1GB/月");
                break;
            default:
                System.out.println("你输入的套餐不存在，请重新输入");
                do{
                    System.out.println("1.话痨套餐 2.网虫套餐 3.超人套餐， 请选择套餐（输入序号）");
                    choose = scanner.nextInt();
                }while(choose>=1&&choose<=3);
                break;
        }
    }

    //本月账单查询
    public void query1(String phone,String key){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)&&arrayList.get(a).getKey().equals(key)){
                System.out.println("****本月账单查询****");
                System.out.println("您的卡号:"+phone+",当月账单:");
                if(arrayList.get(a).getPackages().equals("超人套餐")){
                    System.out.println("套餐资费:"+78+"元");
                }else if(arrayList.get(a).getPackages().equals("网虫套餐")){
                    System.out.println("套餐资费:"+68+"元");
                }else{
                    System.out.println("套餐资费:"+58+"元");
                }
                System.out.println("合计:"+arrayList.get(a).getUse());
                System.out.println("账户余额:"+arrayList.get(a).getMoney());
            }
        }
    }

    //套餐余量查询
    public void query2(String phone,String key){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)&&arrayList.get(a).getKey().equals(key)){
                System.out.println("****套餐余量查询****");
                System.out.println("您的卡号:"+phone+",套餐内剩余:");
                if(arrayList.get(a).getPackages().equals("超人套餐")){
                    int time = 100-arrayList.get(a).getTime()>0?100-arrayList.get(a).getTime():0;
                    System.out.println("通话时长:"+time+"分");
                    int num = 50-arrayList.get(a).getNum()>0?50-arrayList.get(a).getNum():0;
                    System.out.println("短信条数:"+num+"条");
                    double cont = 1024-arrayList.get(a).getCont()>0?(1024-arrayList.get(a).getCont())/1024:0;
                    System.out.println("上网流量:"+cont+"GB");
                }else if(arrayList.get(a).getPackages().equals("网虫套餐")){
                    System.out.println("通话时长:"+0+"分");
                    System.out.println("短信条数:"+0+"条");
                    double cont = 3072-arrayList.get(a).getCont()>0?(3072-arrayList.get(a).getCont())/1024:0;
                    System.out.println("上网流量:"+cont+"GB");
                }else{
                    int time = 500-arrayList.get(a).getTime()>0?500-arrayList.get(a).getTime():0;
                    System.out.println("通话时长:"+time+"分");
                    int num = 30-arrayList.get(a).getNum()>0?30-arrayList.get(a).getNum():0;
                    System.out.println("短信条数:"+num+"条");
                    System.out.println("上网流量:"+0+"GB");
                }
            }
        }
    }

    //打印消费账单
    public void price(String phone){
        String str1 = "******"+phone+"消费记录******";
        System.out.println(str1);
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Wooduan\\嗖嗖1.txt",true),"utf-8"));
            bufferedWriter.newLine();
            bufferedWriter.write(str1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)){
                String str = hashMap.get(phone);
                if(!(str.equals(""))){
                    String [] strs = str.split(" ");
                    for(int i = 0;i<strs.length;i++){
                        if(strs[i].equals("更换话痨套餐消费58元")){
                            System.out.println("因为你本月更换了套餐，所以通话时间，流量，消息条数都被清0，但可以再在具体中查看消费记录");
                        }else if(strs[i].equals("更换网虫套餐消费68元")){
                            System.out.println("因为你本月更换了套餐，所以通话时间，流量，消息条数都被清0，但可以再在具体中查看消费记录");
                        }else if(strs[i].equals("更换超人套餐消费78元")){
                            System.out.println("因为你本月更换了套餐，所以通话时间，流量，消息条数都被清0，但可以再在具体中查看消费记录");
                        }
                    }
                    str1 = "本月通话时间"+arrayList.get(a).getTime()+"分钟";
                    System.out.println(str1);
                    try {
                        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Wooduan\\嗖嗖1.txt",true),"utf-8"));
                        bufferedWriter.newLine();
                        bufferedWriter.write(str1);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            bufferedWriter.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    str1 = "本月上网使用流量"+arrayList.get(a).getCont()+"GB";
                    System.out.println(str1);
                    try {
                        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Wooduan\\嗖嗖1.txt",true),"utf-8"));
                        bufferedWriter.newLine();
                        bufferedWriter.write(str1);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            bufferedWriter.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    str1 = "本月发消息条数"+arrayList.get(a).getNum()+"条";
                    System.out.println(str1);
                    try {
                        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Wooduan\\嗖嗖1.txt",true),"utf-8"));
                        bufferedWriter.newLine();
                        bufferedWriter.write(str1);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            bufferedWriter.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("具体为:");
                    for(int i = 0;i<strs.length;i++){
                        System.out.println(strs[i]);
                    }
                }else{
                    System.out.println("本月还未消费");
                }
            }
        }
    }

    //使用嗖嗖
    public void apply(String phone,String key) throws MoneyException{
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)){
                if(arrayList.get(a).getMoney()>0){
                    int num = -1;
                    do{
                        System.out.println("请问你是要 1.打电话 2.发消息 3.上网 输入其他数字默认退出");
                        num = scanner.nextInt();
                        switch (num){
                            case 1:
                                System.out.println(str[0]);
                                String strs = hashMap.get(phone);
                                strs = strs+str[0];
                                hashMap.put(phone,strs);
                                System.out.println("不存在此卡的消费记录，已添加一条消费记录");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-40);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+40);
                                arrayList.get(a).setTime(arrayList.get(a).getTime()+200);
                                if(arrayList.get(a).getMoney()<=0){
                                    throw new MoneyException("余额不足请充值后再使用");
                                }
                                break;
                            case 2:
                                System.out.println(str[1]);
                                strs = hashMap.get(phone);
                                strs = strs+str[1];
                                hashMap.put(phone,strs);
                                System.out.println("不存在此卡的消费记录，已添加一条消费记录");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-5);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+5);
                                arrayList.get(a).setTime(arrayList.get(a).getNum()+50);
                                if(arrayList.get(a).getMoney()<=0){
                                    throw new MoneyException("余额不足请充值后再使用");
                                }
                                break;
                            case 3:
                                System.out.println(str[2]);
                                strs = hashMap.get(phone);
                                strs = strs+str[2];
                                hashMap.put(phone,strs);
                                System.out.println("不存在此卡的消费记录，已添加一条消费记录");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-100);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+100);
                                arrayList.get(a).setTime(arrayList.get(a).getCont()+1024);
                                if(arrayList.get(a).getMoney()<=0){
                                    throw new MoneyException("余额不足请充值后再使用");
                                }
                                break;
                            default:
                                System.out.println("退出成功");
                                break;
                        }
                    }while(num>=1&&num<=3);
                    break;
                }else{
                    throw new MoneyException("余额不足请充值后再使用");
                }
            }else{
                System.out.println("为查询到该手机号");
            }
        }
    }

    //办理退网
    public void remove(String phone){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)){
                System.out.println("卡号"+phone+"退网成功");
                System.out.println("谢谢使用");
                arrayList.remove(a);
                hashMap.remove(phone);
            }
        }
    }

    //更换套餐
    public void change(String phone){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)){
                System.out.println("****套餐变更****");
                System.out.println("1.话痨套餐 2。网虫套餐 3.超人套餐 请选择（序号）");
                int num = scanner.nextInt();
                switch (num){
                    case 1:
                        if(!(arrayList.get(a).getPackages().equals("话痨套餐"))){
                            if(arrayList.get(a).getMoney()>=58){
                                System.out.println("恭喜你更换套餐成功，消费58元");
                                arrayList.get(a).setPackages("话痨套餐");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-58);
                                arrayList.get(a).setTime(0);
                                arrayList.get(a).setCont(0);
                                arrayList.get(a).setNum(0);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+58);
                                String str = hashMap.get(phone);
                                str = str+"更换话痨套餐消费58元 ";
                                hashMap.put(phone,str);
                            }else{
                                System.out.println("对不起，你的余额不足以支付新套餐本月资费，请重新充值后再办理更换套餐业务！");
                            }
                        }else{
                            System.out.println("对不起，你已经是该套餐的用户，无需更换套餐");
                        }
                        break;
                    case 2:
                        if(!(arrayList.get(a).getPackages().equals("网虫套餐"))){
                            if(arrayList.get(a).getMoney()>=68){
                                System.out.println("恭喜你更换套餐成功，消费68元");
                                arrayList.get(a).setPackages("网虫套餐");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-68);
                                arrayList.get(a).setTime(0);
                                arrayList.get(a).setCont(0);
                                arrayList.get(a).setNum(0);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+68);
                                String str = hashMap.get(phone);
                                str = str+"更换网虫套餐消费68元 ";
                                hashMap.put(phone,str);
                            }else{
                                System.out.println("对不起，你的余额不足以支付新套餐本月资费，请重新充值后再办理更换套餐业务！");
                            }
                        }else{
                            System.out.println("对不起，你已经是该套餐的用户，无需更换套餐");
                        }
                        break;
                    case 3:
                        if(!(arrayList.get(a).getPackages().equals("超人套餐"))){
                            if(arrayList.get(a).getMoney()>=68){
                                System.out.println("恭喜你更换套餐成功，消费78元");
                                arrayList.get(a).setPackages("超人套餐");
                                arrayList.get(a).setMoney(arrayList.get(a).getMoney()-78);
                                arrayList.get(a).setTime(0);
                                arrayList.get(a).setCont(0);
                                arrayList.get(a).setNum(0);
                                arrayList.get(a).setUse(arrayList.get(a).getUse()+78);
                                String str = hashMap.get(phone);
                                str = str+"更换超人套餐消费78元 ";
                                hashMap.put(phone,str);
                            }else{
                                System.out.println("对不起，你的余额不足以支付新套餐本月资费，请重新充值后再办理更换套餐业务！");
                            }
                        }else{
                            System.out.println("对不起，你已经是该套餐的用户，无需更换套餐");
                        }
                        break;
                }
            }
        }
    }

    //话费充值
    public void recharge(String phone,int money){
        for(int a = 0;a<arrayList.size();a++){
            if(arrayList.get(a).getPhone().equals(phone)){
                arrayList.get(a).setMoney(arrayList.get(a).getMoney()+money);
                System.out.println("充值成功,当前余额为"+arrayList.get(a).getMoney());
            }
        }
    }

    //查看资费
    public void check(){
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Wooduan\\嗖嗖.txt"),"utf-8"));
            String str = null;
            while((str = bufferedReader.readLine())!=null){
                System.out.println(str);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
